﻿using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using RuoVea.ExDto;
using RuoVea.ExSugar.Entity;
using RuoVea.ExSugar.UnitOfWork;
using RuoVea.ExUtil;
using RuoVea.OmiApi.Reports.Dto;
using RuoVea.OmiApi.Reports.Models;
using RuoVea.OmiApi.Reports.Service;
using RuoVea.OmiReports.Attributes;
using RuoVea.OmiReports.Language;
using SqlSugar;

namespace RuoVea.OmiReports.Controllers;

/// <summary>
/// 
/// </summary>
[AppRoute]
public class RptDatabaseController : Controller
{
    private readonly RptDatabaseService _DatabaseService;
    /// <summary>
    /// 
    /// </summary>
    /// <param name="DatabaseService"></param> 
    public RptDatabaseController(RptDatabaseService DatabaseService)
    {
        _DatabaseService = DatabaseService;
    }

    /// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    [HttpGet(nameof(Index))]
    public IActionResult Index()
    {
        return View();
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("pages")]
    public async Task<RestfulResult<PageResult<RptDatabase>>> GetPagesAsync()
    {
        using var sr = new StreamReader(Request.Body);
        string dataStr = await sr.ReadToEndAsync();
        if (Request.Body.CanSeek)
            Request.Body.Position = 0;
        if (string.IsNullOrWhiteSpace(dataStr))
            throw new Exception(i18n.categorynotempty);
        RptDatabaseParam data = JsonConvert.DeserializeObject<RptDatabaseParam>(dataStr);
        data ??= new RptDatabaseParam { PageNo = 1, PageSize = 10 };

        var query = await _DatabaseService.GetPagesAsync(data);
        return query;
    }

    /// <summary>
    /// 查询所有/分页
    /// </summary>
    /// <returns></returns>
    [HttpPost("list")]
    public async Task<RestfulResult<PageResult<RptDatabase>>> GetlistAsync()
    {
        return await _DatabaseService.GetlistAsync();
    }

    /// <summary>
    /// 增加
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [UnitOfWork]
    [HttpPost("add")]
    public async Task<RestfulResult> AddAsync([FromBody] RptDatabase data)
    {
        return await _DatabaseService.AddAsync(data);
    }

    /// <summary>
    /// 更新
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpPost("update")]
    public async Task<RestfulResult> UpdateAsync([FromBody] RptDatabase data)
    {
        return await _DatabaseService.UpdateAsync(data);
    }

    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="data"></param>
    /// <returns></returns>
    [HttpDelete("delete")]
    public async Task<RestfulResult> DeleteAsync([FromBody] EntityBaseId data)
    {
        return await _DatabaseService.DeleteAsync(data);
    }

    /// <summary>
    /// 获取数据库类型
    /// </summary>
    /// <returns></returns>
    [HttpGet("getDataType")]
    public RestfulResult GetDataType()
    {
        var dbTypes = EnumExtension.GetDic<DbType>().Take(5).ToList();
        return Restful.Success(dbTypes);
    }

    /// <summary>
    /// 连接测试
    /// </summary>
    /// <param name="parm"></param>
    /// <returns></returns>
    [HttpPost("testConnection")]
    public RestfulResult TestConnection([FromBody] RptDatabase parm)
    {
        try
        {
            bool isSuccess = parm != null && parm.ConnectionString != null && parm.DataType != null && _DatabaseService.GetTestDbConnection(parm.ConnectionString, parm.DataType ?? 0);
            if (isSuccess)
            {
                return Restful.Success("success");
            }
            else
            {
                return Restful.Bad();
            }
        }
        catch (Exception ex)
        {
             return  Restful.Bad(ex.Message);
        }
    }

    /// <summary>
    /// 查询数据库配置信息列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("connectionOptions")]
    public RestfulResult ConnectionOptions()
    {
        return Restful.Success( _DatabaseService.GetConnectionOptions());
    }
}
